<?php
/**
 * Class that operate on table ''.$this->tableName.''. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2012-11-21 10:54
 */
class BibleMySqlDAO implements BibleDAO{

    protected $tableName;

    public function __construct($tableName) {
        $this->tableName = $tableName;
    }
	public function load($id){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM '.$this->tableName;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	

	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM '.$this->tableName.' ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	

	public function delete($id){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->executeUpdate($sqlQuery);
	}

	public function insert($tiBible){
		$sql = 'INSERT INTO '.$this->tableName.' (book, chapter, no, no_start, no_end, Article, PARAGRAPH, IP, entry_date, remark) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($tiBible->book);
		$sqlQuery->setNumber($tiBible->chapter);
		$sqlQuery->setNumber($tiBible->no);
		$sqlQuery->setNumber($tiBible->noStart);
		$sqlQuery->setNumber($tiBible->noEnd);
		$sqlQuery->set($tiBible->article);
		$sqlQuery->setNumber($tiBible->pARAGRAPH);
		$sqlQuery->set($tiBible->iP);
		$sqlQuery->set($tiBible->entryDate);
		$sqlQuery->set($tiBible->remark);

		$id = $this->executeInsert($sqlQuery);	
		$tiBible->id = $id;
		return $id;
	}

	public function update($tiBible){
		$sql = 'UPDATE '.$this->tableName.' SET book = ?, chapter = ?, no = ?, no_start = ?, no_end = ?, Article = ?, PARAGRAPH = ?, IP = ?, entry_date = ?, remark = ? WHERE id = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($tiBible->book);
		$sqlQuery->setNumber($tiBible->chapter);
		$sqlQuery->setNumber($tiBible->no);
		$sqlQuery->setNumber($tiBible->noStart);
		$sqlQuery->setNumber($tiBible->noEnd);
		$sqlQuery->set($tiBible->article);
		$sqlQuery->setNumber($tiBible->pARAGRAPH);
		$sqlQuery->set($tiBible->iP);
		$sqlQuery->set($tiBible->entryDate);
		$sqlQuery->set($tiBible->remark);

		$sqlQuery->setNumber($tiBible->id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM '.$this->tableName.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByBook($value){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE book = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByChapter($book, $chapter){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE book = ? AND chapter = ? ORDER BY book, chapter';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($book);
        $sqlQuery->setNumber($chapter);
		return $this->getList($sqlQuery);
	}

	public function queryByNo($value){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE no = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByNoStart($value){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE no_start = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByNoEnd($value){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE no_end = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByArticle($value){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE Article = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPARAGRAPH($value){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE PARAGRAPH = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByIP($value){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE IP = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEntryDate($value){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE entry_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByRemark($value){
		$sql = 'SELECT * FROM '.$this->tableName.' WHERE remark = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByBook($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE book = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByChapter($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE chapter = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNo($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE no = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNoStart($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE no_start = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByNoEnd($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE no_end = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByArticle($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE Article = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPARAGRAPH($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE PARAGRAPH = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByIP($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE IP = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEntryDate($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE entry_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByRemark($value){
		$sql = 'DELETE FROM '.$this->tableName.' WHERE remark = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	

	protected function readRow($row){
		$tiBible = new Bible();
		
		$tiBible->id = $row['id'];
		$tiBible->book = $row['book'];
		$tiBible->chapter = $row['chapter'];
		$tiBible->no = $row['no'];
		$tiBible->noStart = $row['no_start'];
		$tiBible->noEnd = $row['no_end'];
		$tiBible->article = $row['Article'];
		$tiBible->pARAGRAPH = $row['PARAGRAPH'];
		$tiBible->iP = $row['IP'];
		$tiBible->entryDate = $row['entry_date'];
		$tiBible->remark = $row['remark'];

		return $tiBible;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}

	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>